# coding:utf-8
import os
import datetime
import zipfile
import re


def zipDir(dirpath, outFullName, password=None):
    """
    压缩指定文件夹
    :param dirpath: 目标文件夹路径
    :param outFullName: 保存路径+xxxx.zip
    :return: 
    """
    import os
    if password:
        cmd = "zip -P %s -r %s %s" % (password, outFullName, dirpath)   
    else:
        cmd = "zip -r %s %s" % (outFullName, dirpath)  
    status = os.popen(cmd)

    return outFullName


def gen_pass(length=8, enhance=False):
    """生成默认长度为8的随机密码串
    enhance=True: 加入特殊字符
    """
    import random
    # 去除0、O、o、z、Z、l
    symbols = '!@#$%&*+/\~-_=^'  # 还可以添加特殊字符，加强密码
    if enhance:
        payloads = ''.join([chr(x)+chr(x+32) for x in range(65,91) if x not in [79, 90, 108, 111, 122]]) + '123456789' + symbols
    else:
        payloads = ''.join([chr(x)+chr(x+32) for x in range(65,91) if x not in [79, 90, 108, 111, 122]]) + '123456789'
    return ''.join([random.choice(payloads) for _ in range(length)])


def get_yesterday():
    """
    获取昨天的日期
    """
    today = datetime.date.today()
    first_day = datetime.timedelta(days=1)
    yesterday = today-first_day
    return yesterday


def get_next_month():
    """
    获取下个月日期
    """
    next_month = datetime.datetime.now().replace(day=28) + datetime.timedelta(days=6)
    next_month = next_month.strftime('%Y_%#m') 
    return next_month


def get_prev_month():
    """
    获取上个月信息
    """
    prev_month = datetime.datetime.now().replace(day=1) - datetime.timedelta(days=6)
    prev_month = prev_month.strftime('%Y_%#m')
    return prev_month


def remove_bom(s, d):
    """
    去掉配置文件开头的BOM字节
    """
    content = open(s, 'rb').read(3)

    # content = content.replace(r"\xfe\xff", "", content)
    # content = content.replace(r"\xff\xfe", "", content)
    # content = content.replace(r"\xef\xbb\xbf", "", content)

    if b'\xef\xbb\xbf' == content:
        print("有bom")

        content = open(s, 'rb').read()
        content = content[3:]
        open(d, 'wb').write(content)
    else:
        print("没有bom")


def replace_slash(p):
    """
    替换路径中的 \\ 为 /
    """
    return p.replace('\\', '/')


if __name__ == "__main__":

    print(get_prev_month())
